<template>
  <div id="answer">
    <div class="flexarea">
      <div class="right">
        <div class="title">
          <p>{{ currentQuestionIndex + 1 }} / {{ total }}</p>
          <span>倒计时：<b>{{ time }}</b> 分钟</span>
        </div>
        <div class="question-wrapper">
        <transition :name="transitionName" mode="out-in">
        <div class="content" v-if="currentQuestion">
          <p class="topic">
            <span class="number">{{ currentQuestionIndex + 1 }}</span>
            {{ currentQuestion.content }}
          </p>

          <!-- 选择题 -->
          <div v-if="currentQuestion.type === 1">
            <el-radio-group v-model="answers[currentQuestionIndex]">
              <el-radio :label="'option1'">{{ currentQuestion.option1 }}</el-radio>
              <el-radio :label="'option2'">{{ currentQuestion.option2 }}</el-radio>
              <el-radio :label="'option3'">{{ currentQuestion.option3 }}</el-radio>
              <el-radio :label="'option4'">{{ currentQuestion.option4 }}</el-radio>
            </el-radio-group>
          </div>

          <!-- 填空题 -->
          <div v-else-if="currentQuestion.type === 0">
            <el-input
              placeholder="请输入答案"
              v-model="answers[currentQuestionIndex]"
              clearable
            />
          </div>

          <!-- 判断题 -->
          <div v-else-if="currentQuestion.type === 2">
            <el-radio-group v-model="answers[currentQuestionIndex]">
              <el-radio :label="'T'">正确</el-radio>
              <el-radio :label="'F'">错误</el-radio>
            </el-radio-group>
          </div>
        </div>
        </transition></div>
        <!-- 操作按钮 -->
        <div class="operation">
          <ul class="end">
            <li @click="previous" :disabled="currentQuestionIndex === 0">
              <i class="iconfont icon-previous" /><span>上一题</span>
            </li>
            <li @click="markQuestion">
              <i class="iconfont icon-mark-o" /><span>标记</span>
            </li>
            <li @click="next">
              <span>下一题</span><i class="iconfont icon-next" />
            </li>
          </ul>
          <div class="final" @click="commit">查看答题卡</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      time: 60,
      questionIds: [],
      questions: [],
      currentQuestionIndex: 0,
      answers: [],
      marked: [],
      examId:null,
      transitionName: 'slide-right',
    };
  },
  computed: {
    currentQuestion() {
      return this.questions[this.currentQuestionIndex] || {};
    },
    total() {
      return this.questions.length;
    }
  },
  created() {
    // ✅ 从路由参数读取题目ID
    const queryIds = this.$route.query.questionIds;
    this.questionIds = queryIds ? queryIds.split(',').map(id => parseInt(id)) : [];
    const examId = this.$route.query.examId;
if (examId) {
  this.examId = examId;
  this.$cookies.set('examId', examId);
}    
    // ✅ 设置当前题目序号
    const index = parseInt(this.$route.query.questionIndex);
    if (!isNaN(index)) {
      this.currentQuestionIndex = index;
    }

    // ✅ 读取答案（如果已存在）
    const cookieAnswers = this.$cookies.get('answers');
    if (cookieAnswers && cookieAnswers.length === this.questionIds.length) {
      this.answers = cookieAnswers;
    } else {
      this.answers = Array(this.questionIds.length).fill(null);
      this.$cookies.set('answers', this.answers);
    }

    this.loadQuestions();
    this.startTimer();
  },
  methods: {
    loadQuestions() {
      //const allQuestions = this.$cookies.get('questionList') || [];
      let allQuestions = [];
try {
  const stored = localStorage.getItem('questionList');
  allQuestions = stored ? JSON.parse(stored) : [];
} catch (e) {
  console.error('读取 localStorage 错误:', e);
  this.$message.error('加载题目失败，请重试');
}

      if (!allQuestions.length) {
        this.$message.error('未能加载到题目，请返回试卷详情页重新进入考试');
        return;
      }

      // ✅ 保证题目顺序和 questionIds 一致
      this.questions = this.questionIds.map(id =>
        allQuestions.find(q => String(q.id) === String(id))
      );

      // 再次校准答案长度
      if (this.answers.length !== this.questions.length) {
        this.answers = Array(this.questions.length).fill(null);
        this.$cookies.set('answers', this.answers);
      }
    },
    previous() {
      if (this.currentQuestionIndex > 0) {
        this.transitionName = 'slide-left';
        this.currentQuestionIndex--;
      }
    },
    next() {
      if (this.currentQuestionIndex < this.total - 1) {
        this.transitionName = 'slide-right';
        this.currentQuestionIndex++;
      } else {
        this.commit(); // 最后一题自动跳转答题卡
      }

      this.$cookies.set('answers', this.answers);
    },
    markQuestion() {
      if (!this.marked.includes(this.currentQuestionIndex)) {
        this.marked.push(this.currentQuestionIndex);
      }
    },
    commit() {
      this.$router.push({
        path: '/answerSheet',
        query: {
          questionIds: this.questionIds.join(','),
          answers: JSON.stringify(this.answers),
          marked: JSON.stringify(this.marked),
          time: this.time,
          examID: this.examId
        }
      });
    },
    startTimer() {
      const timer = setInterval(() => {
        if (this.time > 0) {
          this.time--;
        } else {
          clearInterval(timer);
          this.commit(); // 时间到自动提交
        }
      }, 60 * 1000);
    }
  }
};
</script>

<style lang="less" scoped>
.question-wrapper {
  position: relative;
  height: 470px;
  overflow: hidden;
}

/* 滑动动画类 */
.slide-left-enter-active,
.slide-right-enter-active,
.slide-left-leave-active,
.slide-right-leave-active {
  transition: all 0.3s ease;
  position: absolute;
  width: 100%;
  top: 0;
  left: 0;
}

.slide-left-enter-from {
  transform: translateX(-100%);
  opacity: 0;
}
.slide-left-leave-to {
  transform: translateX(100%);
  opacity: 0;
}

.slide-right-enter-from {
  transform: translateX(100%);
  opacity: 0;
}
.slide-right-leave-to {
  transform: translateX(-100%);
  opacity: 0;
}
.iconfont.icon-time {
  color: #2776df;
  margin: 0px 6px 0px 20px;
}
.analysis {
  margin-top: 20px;
  .right {
    color: #2776df;
    font-size: 18px;
    border: 1px solid #2776df;
    padding: 0px 6px;
    border-radius: 4px;
    margin-left: 20px;
  }
  ul li:nth-child(2) {
    margin: 20px 0px;
  }
  ul li:nth-child(3) {
    padding: 10px;
    background-color: #d3c6c9;
    border-radius: 4px;
  }
}
.analysis span:nth-child(1) {
  font-size: 18px;
}
.mark {
  position: absolute;
  width: 4px;
  height: 4px;
  content: "";
  background-color: red;
  border-radius: 50%;
  top: 0px;
  left: 22px;
}
.border {
  position: relative;
  border: 1px solid #FF90AA !important;
}
.bg {
  background-color: #5188b8 !important;
}
.fill .el-input {
  display: inline-flex;
  width: 150px;
  margin-left: 20px;
  .el-input__inner {
    border: 1px solid transparent;
    border-bottom: 1px solid #eee;
    padding-left: 20px;
  }
}
/* slider过渡效果 */
.slider-fade-enter-active {
  transition: all .3s ease;
}
.slider-fade-leave-active {
  transition: all .3s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slider-fade-enter, .slider-fade-leave-to {
  transform: translateX(-100px);
  opacity: 0;
}

.operation .end li:nth-child(2) {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: rgb(39, 118, 223);
  border-radius: 50%;
  width: 50px;
  height: 50px;
  color: #fff;
}
.operation .end li {
  cursor: pointer;
  margin: 0 100px;
}
.operation {
  background-color: #fff;
  border-radius: 4px;
  padding: 10px 0px;
  margin-right: 10px;
}
.operation .end {
  display: flex;
  justify-content: center;
  align-items: center;
  color: rgb(39, 118, 223);
}
.content .number {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  width: 20px;
  height: 20px;
  background-color: rgb(39, 118, 223);
  border-radius: 4px;
  margin-right: 4px;
}
.content {
  padding: 0px 20px;
}
.content .topic {
  padding: 20px 0px;
  padding-top: 30px; 
}
.right .content {
  background-color: #fff;
  margin: 10px;
  margin-left: 0px;
  height: 470px;
}
.content .el-radio-group label {
  color: #000;
  margin: 5px 0px;
}
.content .el-radio-group {
  display: flex;
  flex-direction:column;
}
.right .title p {
  margin-left: 20px;
}
.flexarea {
  display: flex;
}
.flexarea .right {
  flex: 1;
}
.auto-right {
  margin-left: auto;
  color: #2776df;
  margin-right: 10px;
}
.right .title {
  margin-right: 10px;
  padding-right: 30px;
  display: flex;
  margin-top: 10px;
  background-color: #fff;
  height: 50px;
  line-height: 50px;
}
.clearfix {
  clear: both;
}
.l-bottom .final {
  cursor: pointer;
  display: inline-block;
  text-align: center;
 background-color: rgb(39, 118, 223);
  width: 240px;
  margin: 20px 0px 20px 10px;
  border-radius: 4px;
  height: 30px;
  line-height: 30px;
  color: #fff;
  margin-top: 22px;
}
#answer .left .item {
  padding: 0px;
  font-size: 16px;
}
.l-bottom {
  border-radius: 4px;
  background-color: #fff;
}
.l-bottom .item p {
  margin-bottom: 15px;
  margin-top: 10px;
  color: #000;
  margin-left: 10px;
  letter-spacing: 2px;
}
.l-bottom .item li {
  width: 15%;
  margin-left: 5px;
  margin-bottom: 10px;
}
.l-bottom .item {
  display: flex;
  flex-direction: column;
}
.l-bottom .item ul {
  width: 100%;
  margin-bottom: -8px;
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
}
.l-bottom .item ul li a { 
  position: relative;
  justify-content: center;
  display: inline-flex;
  align-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: #fff;
  border: 1px solid #eee;
  text-align: center;
  color: #000;
  font-size: 16px;
}
.left .l-top {
  display: flex;
  justify-content: space-around;
  padding: 16px 0px;
  border: 1px solid #eee;
  border-radius: 4px;
  margin-bottom: 10px;
  background-color: #fff;
}
.left {
  width: 260px;
  height: 100%;
  margin: 10px 10px 0px 10px;
}
.left .l-top li:nth-child(2) a {
  border: 1px solid #eee;
}
.left .l-top li:nth-child(3) a {
  background-color: #5188b8;
  border: none;
}
.left .l-top li:nth-child(4) a {
  position: relative;
  border: 1px solid #eee;
}
.left .l-top li:nth-child(4) a::before {
  width: 4px;
  height: 4px;
  content: " ";
  position: absolute;
  background-color: red;
  border-radius: 50%;
  top: 0px;
  left: 16px;
}
.left .l-top li {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.left .l-top li a {
  display: inline-block;
  padding: 10px;
  border-radius: 50%;
  background-color: #fff;
  border: 1px solid #FF90AA;
}
#answer .top {
  background-color: rgb(39, 118, 223);
}
#answer .item {
  color: #fff;
  display: flex;
  padding: 20px;
  font-size: 20px;
}
#answer .top .item li:nth-child(1) {
  margin-right: 10px;
}
#answer .top .item li:nth-child(3) {
  position: relative;
  margin-left: auto;
}
#answer {
  padding-bottom: 30px;
}
.icon20 {
  font-size: 20px;
  font-weight: bold;
}
.item .msg {
  padding: 10px 15px;
  border-radius: 4px;
  top: 47px;
  right: -30px;
  color: #6c757d;
  position: absolute;
  border: 1px solid rgba(0,0,0,.15);
  background-color: #fff;
}
.item .msg p {
  font-size: 16px;
  width: 200px;
  text-align: left;
}
.operation .end li {
  cursor: pointer;
  margin: 0 100px;
  transition: all 0.3s ease;
  padding: 6px 12px;
  border-radius: 6px;
}

.operation .end li:hover {
  background-color: #005fcc;
  transform: scale(1.08);
  color: #fff;
}

.final {
  cursor: pointer;
  transition: all 0.3s ease;
  background-color: rgb(39, 118, 223);
  color: #fff;
  width: 160px; /* 缩短 */
  height: 36px;
  line-height: 36px;
  border-radius: 6px;
  text-align: center;
  margin: 20px auto 10px;
  font-size: 16px;
}

.final:hover {
  background-color: #005fcc;
  transform: scale(1.05);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  color: #fff;
}
.slide-left-enter-active,
.slide-right-enter-active,
.slide-left-leave-active,
.slide-right-leave-active {
  transition: all 0.4s ease;
  position: absolute;
  width: 100%;
}

.slide-left-enter-from {
  transform: translateX(-100%);
  opacity: 0;
}
.slide-left-leave-to {
  transform: translateX(100%);
  opacity: 0;
}

.slide-right-enter-from {
  transform: translateX(100%);
  opacity: 0;
}
.slide-right-leave-to {
  transform: translateX(-100%);
  opacity: 0;
}
</style>
